/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Service
*        文件名:             LoginService.svc.cs
*        创建系统时间:       2012/11/2 19:07:06
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Service
{
    using System;
    using System.Data;
    using System.Reflection;
    using System.ServiceModel;
    using ECP.DataAccess;
    using ECP.DataAccess.Utilities;
    using ECP.Interface;
    using ECP.Model;
    using ECP.Utilities;

    [ServiceBehavior(Namespace = "http://jiangguang.net.cn/")]
    public partial class LoginService : MarshalByRefObject, ILoginService
    {
        private static LoginService instance = null;
        private static object locker = new object();

        public int ChangePassword(BaseUserInfo userInfo, string oldPassword, string newPassword, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dbHelper.BeginTransaction();
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
                BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
                num = dao.ChangePassword(oldPassword, newPassword, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
                dbHelper.CommitTransaction();
            }
            catch (Exception exception)
            {
                dbHelper.RollbackTransaction();
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int CheckOnLine(BaseUserInfo userInfo)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseUserDao dao = new BaseUserDao(dbHelper);
                dao.OnLine(userInfo.ID);
                num = dao.CheckOnLine();
            }
            catch (Exception exception)
            {
                BaseInterfaceLogic.WriteException(exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public DataTable GetOnLineState(BaseUserInfo userInfo)
        {
            DataTable onLineStateList = new DataTable();
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
                if (MessageService.LaseCheck == DateTime.MinValue)
                {
                    dao.OnLine(userInfo.ID);
                    dao.CheckOnLine();
                    MessageService.LaseCheck = DateTime.Now;
                }
                else
                {
                    TimeSpan span = (TimeSpan)(DateTime.Now - MessageService.LaseCheck);
                    if (((span.Minutes * 60) + span.Seconds) >= BaseSystemInfo.OnLineCheck)
                    {
                        dao.OnLine(userInfo.ID);
                        dao.CheckOnLine();
                        MessageService.LaseCheck = DateTime.Now;
                    }
                }
                onLineStateList = dao.GetOnLineStateList();
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return onLineStateList;
        }

        public DataTable GetUserDT(BaseUserInfo userInfo)
        {
            DataTable table = new DataTable(BaseStaffTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
                dao.CheckOnLine();
                table = dao.GetDT(BaseUserTable.FieldEnabled, "1", BaseUserTable.FieldIsVisible, "1");
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public void Load()
        {
        }

        public BaseUserInfo LoginBySUID(BaseUserInfo userInfo, string suid, out string statusCode, out string statusMessage)
        {
            BaseUserInfo info = null;
            statusCode = string.Empty;
            statusMessage = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
                dao.CheckOnLine();
                info = dao.LoginByUID(suid, userInfo.IPAddress, userInfo.MACAddress, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return info;
        }

        public BaseUserInfo LoginByUserName(BaseUserInfo userInfo, string userName, out string statusCode, out string statusMessage)
        {
            BaseUserInfo info = null;
            statusCode = string.Empty;
            statusMessage = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
                dao.CheckOnLine();
                info = dao.LoginByUserName(userName, userInfo.IPAddress, userInfo.MACAddress, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return info;
        }

        public void OnExit(BaseUserInfo userInfo)
        {
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
                new BaseUserDao(dbHelper, userInfo).OnExit(userInfo.ID);
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
        }

        public int ServerCheckOnLine()
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseUserDao(dbHelper).CheckOnLine();
            }
            catch (Exception exception)
            {
                BaseInterfaceLogic.WriteException(exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int SetPassword(BaseUserInfo userInfo, string[] userIDs, string password, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
                BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
                num = dao.BatchSetPassword(userIDs, password, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public BaseUserInfo UserLogin(BaseUserInfo userInfo, string userName, string password, out string statusCode, out string statusMessage)
        {
            BaseUserInfo info = null;
            statusCode = string.Empty;
            statusMessage = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
                dao.CheckOnLine();
                info = dao.Login(userName, password, userInfo.IPAddress, userInfo.MACAddress, out statusCode);
                if (info != null)
                {
                    info.IsAdministrator = PermissionService.Instance.IsAdministratorByUser(dbHelper, userInfo, info.ID);
                }
                statusMessage = dao.GetStateMessage(statusCode);
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return info;
        }

        public static LoginService Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new LoginService();
                        }
                    }
                }
                return instance;
            }
        }
    }
}